package com.second.app.excutor;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author GYZ
 * @Date 2024/1/24
 * <pre>
 *          AbstractExecutorService implements ExecutorService{
 *
 *          }
 *          ThreadPoolExecutor extends AbstractExecutorService{
 *
 *          }
 * </pre>
 */

//从打印结果输出来看，A和B几乎是在相同的时间开始begin的，也就是创建2个线程，两个线程之间是异步运行的
public class example {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.execute(() -> {
            System.out.println("Runnable begin " + System.currentTimeMillis());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("A");
            System.out.println("Runnable end " + System.currentTimeMillis());
        });

        executorService.execute(() -> {
            System.out.println("Runnable2 begin " + System.currentTimeMillis());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("B");
            System.out.println("Runnable2 end " + System.currentTimeMillis());
        });
    }
}

//循环打印也成功
class Run2 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 5; i++) {
            executorService.execute(() -> System.out.println("run!!!!"));
        }
    }
}

//从打印结果来看，对象创建是完全成功的，但是还没有达到线程池中对象可以复用的效果。
class Run3 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 10; i++) {
            int index = i;
            executorService.execute(() -> {
                System.out.println(Thread.currentThread().getName() + " i=" + (index + 1) + " begin" + System.currentTimeMillis());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " i=" + (index + 1) + " end" + System.currentTimeMillis());
            });
        }
    }
}

//线程复用
class Run4 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();
        for (int i = 0; i < 5; i++) {
            int index = i;
            executorService.execute(() -> {
                System.out.println(Thread.currentThread().getName() + " i=" + (index + 1) + " begin" + System.currentTimeMillis());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " i=" + (index + 1) + " end" + System.currentTimeMillis());
            });
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("================");
        System.out.println("================");
        for (int i = 0; i < 5; i++) {
            int index = i;
            executorService.execute(() -> {
                System.out.println(Thread.currentThread().getName() + " i=" + (index + 1) + " begin" + System.currentTimeMillis());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " i=" + (index + 1) + " end" + System.currentTimeMillis());
            });
        }
    }
}


